<template>
  <div class="gas-set">
    <div class="top flex-left">
      <el-button type="primary" size="medium" @click="detail.type = 0; showDialog = true">新增币种</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="medium"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" label="币种名称" prop="coin_name"></el-table-column>
        <el-table-column align="center" prop="low" label="最低手续费"></el-table-column>
        <el-table-column align="center" prop="average" label="最优手续费"></el-table-column>
        <el-table-column align="center" prop="high" label="最高手续费"></el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="item">
            <el-button type="text" @click="edit(item.row)" class="edit-btn">编辑</el-button>
            <el-button type="text" style="color:red" @click="del(item.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages :pages="searchForm" :total="total" @pageChange="pageChange" layout="prev, pager, next"></pages>
      <!-- 增加、编辑窗口 -->
      <gas-set v-if="showDialog" :detail="detail" @cancel="showDialog = false" @success="success"></gas-set>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import GasSet from "@/components/CoinsManage/GasSet.vue";
import { gasList, gasConfig } from "./Coins.service";
export default {
  components: {
    Pages,
    GasSet
  },
  data() {
    return {
      loading: false,
      tableData: [],
      showDialog: false,
      total: 0,
      detail: {
        type: 0, // 0-新增，1-编辑
        data: {}
      },
      searchForm: {
        page: 1,
        size: 10,
        platform_id: this.$store.state.userInfo.platform_id
      }
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    async search() {
      this.loading = true;
      const res = await gasList();
      this.tableData = res.data || [];
      this.loading = false;
    },
    del(item) {
      this.$confirm(
        `是否删除 <span class="confirm-tip-color">${item.coin_name}</span> 币种?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true
        }
      )
        .then(() => {
          gasConfig('delete', { id: item.id }).then(res => {
            if (res.code === 0)
              this.$notify.success({
                title: "提示",
                message: "删除成功",
                duration: 2000
              });
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    edit(item) {
      console.log(item);
      this.detail = {
        type: 1,
        data: item
      }
      this.showDialog = true
    },
    success() {
      this.showDialog = false
      this.search();
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(this.searchForm);
    }
  }
};
</script>

<style lang="scss">
.gas-set {
  position: relative;
  min-height: calc(100vh - 100px);
  .top {
    .input-with-select {
      width: 241px;
      margin-right: 20px;
      .el-input-group__append {
        background-color: #409eff;
        color: #fff;
      }
    }
  }
  .data-table {
    position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 20px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>